package com.hostmanagement.login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hostmanagement.user.UserBean;

/**
 * 
 * @author glen
 * 
 */
public class LoginServlet extends HttpServlet {
	
	private static final String INVALIDATE_USER = "invalide_user";

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		doPost(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		String userName = req.getParameter("userName");
		String userPass = req.getParameter("userPass");		
		String redirectURL = null;
		
		LoginBean loginBean = new LoginBean();
		UserBean userBean = loginBean.getUser(userName, userPass);
		
		if(userBean != null) {
			HttpSession session = req.getSession();
			session.setAttribute("userBean", userBean);
			redirectURL = "frame.jsp";				
		} else {
			redirectURL = "index.jsp?errorCode=" + INVALIDATE_USER;
		}
		System.out.println(" ======= " + redirectURL);
		res.sendRedirect(redirectURL);
	}
}
